﻿using ProjectScheduling.daos;
using ProjectScheduling.models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectScheduling
{
    public partial class SetPreparedForm : Form
    {
        public static SetPreparedForm b = new();
        private Projects BaseData = new();
        public Action? action;
        private SetPreparedForm()
        {
            InitializeComponent();
            txt_start.ValueChanged += (o, e) =>
            {
                txt_end.MinDate = BaseData.FromDate = txt_start.Value;
                Calc();
            };
            txt_end.ValueChanged += (o, e) =>
            {
                BaseData.ToDate = txt_end.Value;
                Calc();
            };

            txt_start.MinDate = DateTime.Today;
            txt_end.MinDate = DateTime.Today;

        }
        private void Calc()
        {
            if (BaseData == null) return;
            BaseData.Days = Convert.ToInt32(txt_days.Value = Convert.ToInt32((txt_end.Value.Date - txt_start.Value.Date).TotalDays + 1));
        }
        public void Init(int id)
        {
            BaseData = ProjectDao.Base.Find(id);
            if (BaseData == null) return;
            var start = DateTime.Today;
            var end = start.AddHours(18);
            txt_type.Text = BaseData.ProjectType?.Name ?? "";
            txt_customername.Text = BaseData.CustomerName;
            txt_linker.Text = BaseData.Linker;
            txt_phone.Text = BaseData.Phone;
            txt_name.Text = BaseData.Name;
            txt_address.Text = BaseData.Address;
            txt_contract.Text = BaseData.ContractState;
            txt_approve.Text = BaseData.ApproveState;
            txt_saler.Text = BaseData.Sales?.TrueName ?? "";
            txt_start.Value = BaseData.FromDate = start;
            txt_end.Value = BaseData.ToDate = end;
            txt_description.Text = BaseData.Description;
            txt_end.MinDate = txt_start.Value;
            txt_users.Text = "";
            txt_ownership.Text = BaseData.OwnerShip ?? "";
        }

        private void Btn_save_Click(object sender, EventArgs e)
        {
            if (BaseData.Staffs == null || BaseData.Staffs.Count == 0)
            {
                MessageBox.Show("请选择实施人员", "错误提示");
                return;
            }
            if (BaseData.FromDate < DateTime.Now)
            {
                MessageBox.Show("开始时间不能早于当前时间");
                return;
            }
            if (BaseData.ToDate <= BaseData.FromDate)
            {
                MessageBox.Show("结束时间不能小于等于开始时间");
                return;
            }
            if (MessageBox.Show("是否确定要排期", "排期确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                BaseData.State = Enum_ProjectState.已排期;
                ProjectDao.Base.Update(BaseData, it => new { it.FromDate, it.ToDate, it.Days, it.Description, it.State });
                ProjectStaffDao.Base.Delete(it => it.ProjectId == BaseData.Id);
                foreach ((int id, string name) in BaseData.Staffs)
                {
                    ProjectStaffDao.Base.Add(new ProjectStaff
                    {
                        ProjectId = BaseData.Id,
                        UserId = id,
                        UserName = name
                    });
                }
            }
            action?.Invoke();
            Close();
            MessageBox.Show("排期成功");
        }

        private void Txt_times_Click(object sender, EventArgs e)
        {
            CheckTimesForm.b.Init([]);
            CheckTimesForm.b.ShowDialog();
        }

        private void Txt_users_Click(object sender, EventArgs e)
        {
            CheckUsedStaffForm.b.action_checked = (e) =>
            {
                BaseData.Staffs = e;
                txt_users.Text = string.Join(",", e.Select(it => it.name));
            };
            CheckUsedStaffForm.b.GetStaffByStartEnd(BaseData.FromDate, BaseData.ToDate, BaseData.Id);
            CheckUsedStaffForm.b.GetProposeStaffByStartEnd(BaseData.FromDate, BaseData.ToDate);
            CheckUsedStaffForm.b.ShowDialog();
        }
    }
}
